using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace TestCecile
{
    public partial class AankoopFiche : Form
    {
        private int vKlNr;
        private string vKlantInfo;
        private int maxFicheNr;
        private int maxVolgNr;
        private DataRowView drv;
        private DataGridViewCellStyle dataGridViewCellStyle = new DataGridViewCellStyle();

        public AankoopFiche(int vKlNr, string vKlantInfo, DataRowView drv)
        {
            InitializeComponent();
            this.vKlNr = vKlNr;
            this.vKlantInfo = vKlantInfo;
            this.drv = drv;
            //Text = " " + this.vKlNr;
            dataGridViewCellStyle.BackColor = System.Drawing.Color.LightGray;
            dataGridViewCellStyle.ForeColor = System.Drawing.Color.Red;
        }

        private void AankoopFiche_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'dataSet1.KLANT' table. You can move, or remove it, as needed.
            //this.kLANTTableAdapter.Fill(this.dataSet1.KLANT);
            // TODO: This line of code loads data into the 'dataSet1.AANKOOP' table. You can move, or remove it, as needed.
            this.aANKOOPTableAdapter.FillByKlNr(this.dataSet1.AANKOOP,double.Parse(vKlNr.ToString()));
            lblKlantInfo.Text = vKlantInfo;
            int vNr = 0;
            int vFicheNr = 0;
            for (int i = 0; i < dgvAankoopFiche.Rows.Count; i++)
            {
                vNr = int.Parse(dgvAankoopFiche.Rows[i].Cells["dgvNr"].Value.ToString());
                vFicheNr = int.Parse(dgvAankoopFiche.Rows[i].Cells["dgvFicheNr"].Value.ToString());
                if (vNr == 5)
                {
                    dgvAankoopFiche.Rows[i].DefaultCellStyle = dataGridViewCellStyle;
                }
            }
            maxFicheNr = vFicheNr;
            maxVolgNr = vNr;
            aANKOOPBindingSource.MoveLast();
            lblKortingInfo.Text = "";
            if (vNr == 4) lblKortingInfo.Text = "LAATSTE LIJN VAN FICHE!!!";
            if (vNr == 5) lblKortingInfo.Text = "EERSTE LIJN VAN NIEUWE FICHE!!!";
            txtBedrag.Focus();
        }

        private void btnRegistreer_Click(object sender, EventArgs e)
        {
            int vFicheNr = maxFicheNr;
            int vVolgNR = maxVolgNr + 1;
            if (vFicheNr == 0 && vVolgNR == 1)
                vFicheNr++; ;
            if (vVolgNR > 5)
            {
                vFicheNr++;
                vVolgNR = 1;
            }
            //Text = "Fiche " + vFicheNr + " " + vVolgNR;
            try
            {
                int vAankNr = int.Parse(aANKOOPTableAdapter.GetMaxAankNr().ToString())+1;
                //Text = " " + vAankNr;
                aANKOOPTableAdapter.InsertAankoop(vAankNr, 0, dtpDatum.Value, double.Parse(txtBedrag.Text.ToString()), vKlNr, vFicheNr, vVolgNR);
                kLANTTableAdapter.UpdateAankoop(vFicheNr, dtpDatum.Value, vKlNr);
                drv["DATLAATST"] = dtpDatum.Value.ToShortDateString();
                drv["AKORTING"] = vFicheNr;
                // update fiche
                this.aANKOOPTableAdapter.FillByKlNr(this.dataSet1.AANKOOP, double.Parse(vKlNr.ToString()));
                //lblKlantInfo.Text = vKlantInfo;
                int vNr = 0;
                vFicheNr = 0;
                for (int i = 0; i < dgvAankoopFiche.Rows.Count; i++)
                {
                    vNr = int.Parse(dgvAankoopFiche.Rows[i].Cells["dgvNr"].Value.ToString());
                    vFicheNr = int.Parse(dgvAankoopFiche.Rows[i].Cells["dgvFicheNr"].Value.ToString());
                    if (vNr == 5)
                    {
                        dgvAankoopFiche.Rows[i].DefaultCellStyle = dataGridViewCellStyle;
                    }
                }
                maxFicheNr = vFicheNr;
                maxVolgNr = vNr;
                aANKOOPBindingSource.MoveLast();
                lblKortingInfo.Text = "";
                if (vNr == 4) lblKortingInfo.Text = "LAATSTE LIJN VAN FICHE!!!";
                if (vNr == 5) lblKortingInfo.Text = "EERSTE LIJN VAN NIEUWE FICHE!!!";
                txtBedrag.Text = "";
                txtBedrag.Focus();

                //Close();
            }
            catch (Exception ex) { MessageBox.Show("Er is een foutief bedrag ingevuld"); }
                
        }

        private void btnSluit_Click(object sender, EventArgs e)
        {
            int mFicheNr = -1;
            string mDatum = "";
            int aantal = aANKOOPBindingSource.Count;
            DataRowView drv = null;
           //string hulp = " ";
            for (int i = 0; i < aantal;i++)
            {
                drv = this.aANKOOPBindingSource[i] as DataRowView;
                int vFicheNr = int.Parse(drv["FICHENR"].ToString());
                int vAanknr = int.Parse(drv["AANKNR"].ToString());
                double vBedrag = double.Parse(drv["BEDRAG"].ToString());
                string sBedrag = vBedrag.ToString().Replace(",", ".");
                string vDatum = "1/1/2000";
                try
                {
                    vDatum = DateTime.Parse(drv["AANKDATUM"].ToString()).ToShortDateString();
                }
                catch (Exception) { }
                if (vBedrag > 0)
                {
                    mFicheNr = vFicheNr;
                    mDatum = vDatum;
                }
                registreerAankoopUpdate(vAanknr, sBedrag, vDatum);
                //hulp += vAanknr + " " + vBedrag + " " + vDatum+Environment.NewLine;
            }
            //MessageBox.Show(hulp);
            try
            {
                kLANTTableAdapter.UpdateAankoop(mFicheNr, DateTime.Parse(mDatum), vKlNr);
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
            Close();
        }

        private void registreerAankoopUpdate(int vAanknr, string vBedrag, string vDatum)
        {
            try
            {
                
                System.Data.Odbc.OdbcConnection connect;
                System.Data.Odbc.OdbcCommand cmd;
                connect = new System.Data.Odbc.OdbcConnection();
                connect.ConnectionString = Properties.Settings.Default.ConnectionString;
                string cmdString = "UPDATE AANKOOP SET " +
                    "BEDRAG = " + vBedrag.Trim() +", " +
                    "AANKDATUM = " + "'" + vDatum + "'" +
                    " WHERE AANKNR = " + vAanknr;

                //MessageBox.Show(cmdString);
                cmd = new System.Data.Odbc.OdbcCommand(cmdString, connect);
                try
                {
                    connect.Open();
                    cmd.ExecuteNonQuery();
                }
                finally
                {
                    connect.Close();
                }
            }
            catch (Exception ex) {  MessageBox.Show(ex.Message); }
        }

    }
}